/**
 * Created by lional on 16/10/3.
 */

jGame.AppObject = jGame.GameObject.extend(/** @lends jGame.AppObject# */{

    _scenesStack:null,
    _objectsStack:null,

    ctor: function(){
        this._super();
        this._scenesStack = [];
        this._objectsStack = [];
        //cc.log("jGame.AppObject#ctor()");
    },

    runScene: function (scene_json) {
        var sceneNode = jGame.loadWithVisibleSize(scene_json, "res/");

        var controller = this.getObject(cc.path.basename(scene_json, cc.path.extname(scene_json)));
        var scene = new jGame.Scene(controller);
        while (sceneNode.node.getChildrenCount() > 0) {
            var child = sceneNode.node.getChildren()[0];
            child.removeFromParent();
            jGame.setAllChildrenController(child, controller);
            scene.addChild(child);
        }

        scene.setName(cc.path.basename(scene_json, cc.path.extname(scene_json)));

        cc.director.runScene(scene);
        var i = this._scenesStack.length;
        if (i === 0) {
            this._scenesStack[i] = scene;
        } else {
            this._scenesStack[i - 1] = scene;
        }

        if(scene && typeof scene["beforeEnter"] === "function") {
            scene["beforeEnter"]();
        }
    },

    pushScene: function (scene_json) {
        var sceneNode = jGame.loadWithVisibleSize(scene_json, "res/");

        var controller = this.getObject(cc.path.basename(scene_json, cc.path.extname(scene_json)));
        var scene = new jGame.Scene(controller);
        while (sceneNode.node.getChildrenCount() > 0) {
            var child = sceneNode.node.getChildren()[0];
            child.removeFromParent();
            jGame.setAllChildrenController(child, controller);
            scene.addChild(child);
        }

        scene.setName(cc.path.basename(scene_json, cc.path.extname(scene_json)));

        cc.director.pushScene(scene);
        this._scenesStack.push(scene);

        if(scene && typeof scene["beforeEnter"] === "function") {
            scene["beforeEnter"]();
        }
    },

    popScene: function () {

        var scene = this.getRunningScene();

        if(scene && typeof scene["beforeExit"] === "function") {
            scene["beforeExit"]();
        }

        this._scenesStack.pop();
        cc.director.popScene();
    },

    getRunningScene: function () {
        var i = this._scenesStack.length;
        if (i === 0) {
            return this._scenesStack[i];
        } else {
            return this._scenesStack[i - 1];
        }
    },

    resume: function (scene) {
        cc.director.resume();
    },

    pause: function (scene) {
        cc.director.pause();
    },
    
    getView: function (name, userData, classType) {
        var scene = this.getRunningScene();
        return jGame.findView(scene, name, userData, classType);
    },

    setObject: function (name, obj) {
        obj.setName(name);
        this._objectsStack[name] = obj;
    },

    getObject: function (name) {
        return this._objectsStack[name];
    },

    bindClick: function (viewName, clickName, controllerName) {
        var view = (typeof viewName === "string")?app.getView(viewName):viewName;
        if(view && typeof view["setClickCallbackName"] === "function") {
            if(controllerName && typeof view["setController"] === "function") {
                var controller = controllerName instanceof jGame.ControllerObject?controllerName:app.getObject(controllerName);
                if(controller instanceof jGame.ControllerObject) {
                    view["setController"](controller);
                }
            }
            view["setClickCallbackName"](clickName);
        }
    },

    bindTouch: function (viewName, touchName, controllerName) {
        var view = (typeof viewName === "string")?app.getView(viewName):viewName;
        if(view && typeof view["setTouchCallbackName"] === "function") {
            if(controllerName && typeof view["setController"] === "function") {
                var controller = controllerName instanceof jGame.ControllerObject?controllerName:app.getObject(controllerName);
                if(controller instanceof jGame.ControllerObject) {
                    view["setController"](controller);
                }
            }
            view["setTouchCallbackName"](touchName);
        }
    },

    bindSelect: function (viewName, touchName, controllerName) {
        var view = (typeof viewName === "string")?app.getView(viewName):viewName;
        if(view && typeof view["setSelectCallbackName"] === "function") {
            if(controllerName && typeof view["setController"] === "function") {
                var controller = controllerName instanceof jGame.ControllerObject?controllerName:app.getObject(controllerName);
                if(controller instanceof jGame.ControllerObject) {
                    view["setController"](controller);
                }
            }
            view["setSelectCallbackName"](touchName);
        }
    },

    bindOneByOne: function (viewName, touchBeganName, touchMovedName, touchEndedName, touchCancelledName, touchSwallowEnabled, controllerName) {
        var view = (typeof viewName === "string")?app.getView(viewName):viewName;
        if(view && typeof view["setOneByOneCallbackName"] === "function") {
            if(controllerName && typeof view["setController"] === "function") {
                var controller = controllerName instanceof jGame.ControllerObject?controllerName:app.getObject(controllerName);
                if(controller instanceof jGame.ControllerObject) {
                    view["setController"](controller);
                }
            }
            view["setOneByOneCallbackName"](touchBeganName, touchMovedName, touchEndedName, touchCancelledName, touchSwallowEnabled);
        }
    },

    bindAllAtOnce: function (viewName, touchBeganName, touchMovedName, touchEndedName, touchCancelledName, controllerName) {
        var view = (typeof viewName === "string")?app.getView(viewName):viewName;
        if(view && typeof view["setAllAtOnceCallbackName"] === "function") {
            if(controllerName && typeof view["setController"] === "function") {
                var controller = controllerName instanceof jGame.ControllerObject?controllerName:app.getObject(controllerName);
                if(controller instanceof jGame.ControllerObject) {
                    view["setController"](controller);
                }
            }
            view["setAllAtOnceCallbackName"](touchBeganName, touchMovedName, touchEndedName, touchCancelledName);
        }
    },

    cleanObjects: function () {
        for(var objectIndex in this._objectsStack) {
            if(this._objectsStack[objectIndex] instanceof jGame.GameObject) {
                if (typeof this._objectsStack[objectIndex]["removeAllEventListeners"] === "function") {
                    this._objectsStack[objectIndex]["removeAllEventListeners"]();
                }
            }
        }
        this._objectsStack.length = 0;
    },

    save: function () {
        for (var objectIndex in this._objectsStack) {
            if(this._objectsStack[objectIndex] instanceof jGame.ModelObject) {
                if(typeof this._objectsStack[objectIndex]["save"] === "function") {
                    this._objectsStack[objectIndex]["save"]();
                }
            }
        }
    },


});